home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 19 / develop 19 code / GX Bitmaps / BitmapImageAccess.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-21  |  3.1 KB  |  137 lines  |  [TEXT/KAHL]

  1. /*
  2. ••••••••••••••••••••
  3.     BitmapImageAccess.c
  4.     David Surovell
  5.  
  6.     this file contains a routine that computes a histogram for
  7.     any indexed (1-8 bits per pixel) gxBitmap shape
  8. ••••••••••••••••••••
  9. */
  10.  
  11. #include <Types.h>
  12. #include <Memory.h>
  13.  
  14. #include "graphics types.h"
  15. #include "graphics errors.h"
  16. #include "graphics routines.h"
  17.  
  18.  
  19. unsigned long *BitmapShape_GetIndexedCounts(
  20.     gxShape    sourceBits );
  21.  
  22.  
  23.  
  24. unsigned long *BitmapShape_GetIndexedCounts(
  25.     gxShape    sourceBits )
  26. {
  27. gxBitmap            sourceInfo, *sourceInfoRef;
  28. gxShapeAttribute    curAttributes;
  29. OSErr            errStat;
  30. unsigned char        *sourcePtr, *rowPtr, curPixValue;
  31. unsigned long        *curHistoData;
  32. long                histoDataSize, sourceRowSize, structLen, i, j;
  33. Boolean            isQDGXImage;
  34.  
  35.     /* make sure that this is a bitmap shape */
  36.     if (sourceBits == nil)
  37.         return nil;
  38.     if (GXGetShapeType( sourceBits ) != gxBitmapType)
  39.         return nil;
  40.  
  41.     /* make sure that this is an indexed bitmap shape */
  42.     GXGetBitmap( sourceBits, &sourceInfo, nil );
  43.     if ((sourceInfo.pixelSize < 1) || (sourceInfo.pixelSize > 8))
  44.         return nil;
  45.  
  46.     /* if the image data was allocated by QDGX */
  47.     isQDGXImage = ((sourceInfo.image == nil) || (sourceInfo.rowBytes <= 0));
  48.     if (isQDGXImage)
  49.     {
  50.         /* load and lock image data */
  51.         curAttributes = GXGetShapeAttributes( sourceBits );
  52.         if (!(curAttributes & gxDirectShape))
  53.             GXSetShapeAttributes( sourceBits, curAttributes | gxDirectShape );
  54.         GXLockShape( sourceBits );
  55.  
  56.         /* get a reference to image data */
  57.         sourceInfoRef = (gxBitmap*)GXGetShapeStructure( sourceBits, &structLen );
  58.         if ((sourceInfoRef == nil) || (structLen < sizeof(gxBitmap)))
  59.             return nil;
  60.  
  61.         sourceInfo = *sourceInfoRef;
  62.     }
  63.  
  64.     /* allocate a buffer big enough to hold index counts */
  65.     histoDataSize = (1L << sourceInfo.pixelSize) * sizeof(long);
  66.     curHistoData = (unsigned long*)NewPtrClear( histoDataSize );
  67.     errStat = MemError();
  68.     if ((curHistoData == nil) || (errStat != noErr))
  69.         return nil;
  70.  
  71.     /* count index values, one row at a time */
  72.     sourcePtr = (unsigned char*)(sourceInfo.image);
  73.     for (i=sourceInfo.height; i>0; i--)
  74.     {
  75.         rowPtr = sourcePtr;
  76.         sourceRowSize = sourceInfo.width;
  77.         while (sourceRowSize-- > 0)
  78.         {
  79.             curPixValue = *rowPtr;
  80.             switch (sourceInfo.pixelSize)
  81.             {
  82.                 case 8:
  83.                     curHistoData[curPixValue]++;
  84.                     break;
  85.  
  86.                 case 4:
  87.                     for (j=2; j>0; j--)
  88.                         if (sourceRowSize > 0)
  89.                         {
  90.                             curHistoData[curPixValue & 0x0F]++;
  91.                             curPixValue >>= 4;
  92.                             sourceRowSize--;
  93.                         }
  94.                     break;
  95.  
  96.                 case 2:
  97.                     for (j=4; j>0; j--)
  98.                         if (sourceRowSize > 0)
  99.                         {
  100.                             curHistoData[curPixValue & 0x03]++;
  101.                             curPixValue >>= 2;
  102.                             sourceRowSize--;
  103.                         }
  104.                     break;
  105.  
  106.                 case 1:
  107.                     for (j=8; j>0; j--)
  108.                         if (sourceRowSize > 0)
  109.                         {
  110.                             curHistoData[curPixValue & 0x01]++;
  111.                             curPixValue >>= 1;
  112.                             sourceRowSize--;
  113.                         }
  114.                     break;
  115.             }
  116.  
  117.             rowPtr++;
  118.         }
  119.  
  120.         /* skip to the next row */
  121.         sourcePtr = (unsigned char*)sourcePtr + sourceInfo.rowBytes;
  122.     }
  123.  
  124.     /* if the image data was allocated by QDGX */
  125.     if (isQDGXImage)
  126.     {
  127.         /* release the shape */
  128.         GXUnlockShape( sourceBits );
  129.         if (!(curAttributes & gxDirectShape))
  130.             GXSetShapeAttributes( sourceBits, curAttributes );
  131.     }
  132.  
  133.     return curHistoData;
  134. }
  135.  
  136.  
  137.